let socket
function doConnect() {
  socket = new WebSocket('ws://localhost:13528')
  // 如果是https的话，端口是13529
  // socket = new WebSocket('wss://localhost:13529');
  // 打开Socket
  socket.onopen = function(event) {
    console.log('连接成功')
    // 监听消息
    /**
     * 请求打印机列表demo
     * */
    const request = getRequestObject('getPrinters')
    socket.send(JSON.stringify(request))

    // socket.onmessage = function (event) {
    //   console.log(JSON.parse(event.data));
    //   let res = JSON.parse(event.data)
    //   if (res.cmd === 'getPrinters') {

    //   }

    // };
    // 监听Socket的关闭
    socket.onclose = function(event) {
      console.log('Client notified socket has closed', event)
    }
  }
  return socket
}
/** *
 *
 * 获取请求的UUID，指定长度和进制,如
 * getUUID(8, 2)   //"01001010" 8 character (base=2)
 * getUUID(8, 10) // "47473046" 8 character ID (base=10)
 * getUUID(8, 16) // "098F4D35"。 8 character ID (base=16)
 *
 */
function getUUID(len, radix) {
  const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  const uuid = []
  let i
  radix = radix || chars.length
  if (len) {
    for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)]
  } else {
    let r
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
    uuid[14] = '4'
    for (i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | (Math.random() * 16)
        uuid[i] = chars[i === 19 ? (r & 0x3) | 0x8 : r]
      }
    }
  }
  return uuid.join('')
}
/** *
 * 构造request对象
 */
function getRequestObject(cmd) {
  const request = {}
  request.requestID = getUUID(8, 16)
  request.version = '1.0'
  request.cmd = cmd
  return request
}
/** *
 * 获取自定义区数据以及模板URL
 * waybillNO 电子面单号
 */
// function getCustomAreaData(waybillNO) {
//   // 获取waybill对应的自定义区的JSON object，此处的ajaxGet函数是伪代码
//   const jsonObject = waybillNO
//   const ret = {}
//   ret.templateURL = jsonObject.content.templateURL
//   ret.data = jsonObject.content.data
//   return ret
// }
/** *
 * 获取电子面单Json 数据
 * waybillNO 电子面单号
 */
// function getWaybillJson(waybillNO) {
//   //获取waybill对应的json object，此处的ajaxGet函数是伪代码
//   var jsonObject = ajaxGet(waybillNO);
//   return jsonObject;
// }
/**
 * 打印电子面单
 * printer 指定要使用那台打印机
 * waybillArray 要打印的电子面单的数组
 */
function doPrint(arr, url, printer, tip, preview = false) {
  const request = getRequestObject('print')
  request.task = {}
  request.task.taskID = getUUID(8, 10)
  request.task.preview = preview
  request.task.printer = printer || ''
  request.task.previewType = 'image'
  const documents = []
  arr.forEach((i) => {
    const obj = {}
    obj.documentID = i.tid + '|' + i.waybillCode
    const templateURL = i.templateUrl ? i.templateUrl : url
    obj.contents = [
      {
        data: i,
        templateURL: templateURL
      }
    ]
    documents.push(obj)
  })
  request.task.documents = documents
  socket.send(JSON.stringify(request))
}
// 订单打印
function doPrintHandover(data, printerName = '') {
  const request = getRequestObject('print')
  request.task = {}
  request.task.taskID = getUUID(8, 10)
  request.task.preview = false
  request.task.printer = printerName
  request.task.previewType = 'image'
  const documents = []
  data.forEach((i) => {
    // eslint-disable-next-line no-new-object
    const obj = new Object()
    console.log(i.tid)
    obj.documentID = i.tid + '|' + i.waybillCode
    const templateURL = i.templateURL
    obj.contents = [
      {
        data: i,
        templateURL: templateURL
      }
    ]
    documents.push(obj)
  })
  request.task.documents = documents
  socket.send(JSON.stringify(request))
}
function doPrintPdf(arr, url, printer) {
  const request = getRequestObject('print')
  request.task = {}
  request.task.taskID = getUUID(8, 10)
  request.task.preview = true
  // request.task.notifyType = ['print'];
  request.task.printer = printer || ''
  request.task.previewType = 'pdf'
  const documents = []
  arr.forEach((i) => {
    const obj = {}
    obj.documentID = i.documentID
    obj.contents = [
      {
        data: i.content,
        templateURL: url
      }
    ]
    documents.push(obj)
  })
  request.task.documents = documents
  socket.send(JSON.stringify(request))
}
export { doConnect, doPrint, doPrintHandover, doPrintPdf }
